﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hartley.Entities;
using Hartley.DTOs;

namespace Hartley.BusinessLogic
{
   public class SportsManager
    {
       HCPPADataContext hcDc;

       public SportsManager(HCPPADataContext hcDc)
        {
            this.hcDc = hcDc;
        }


       public void SaveTeam(TeamDTO teamDTO)
       {
           var team1 = hcDc.hcTeams.Where(n => n.TeamId == teamDTO.TeamId).SingleOrDefault();
           if (team1 == null)
           {
               var team2 = hcDc.hcTeams.Where(a => a.AgeDivision == teamDTO.AgeDivision && a.Year == teamDTO.Year && a.GameType == teamDTO.GameType).SingleOrDefault();
               if (team2 == null)
               {
                   hcTeam team = new hcTeam
                   {
                       TeamId = teamDTO.TeamId,
                       GameType = teamDTO.GameType,
                       AgeDivision = teamDTO.AgeDivision,
                       Year = teamDTO.Year,
                       Description = teamDTO.Description,
                       TeamPhoto = teamDTO.TeamPhoto,
                       ModifiedBy = teamDTO.ModifiedBy,
                       ModifiedTime = teamDTO.ModifiedTime
                   };
                   hcDc.hcTeams.InsertOnSubmit(team);
               }
               else
               {
                   //IF TEAM  repeats don't add them.(i.e yr,game type,age division
               }

           }
           else
           {
               //Edit
           }

           hcDc.SubmitChanges();
       }



       public void SaveTeamMembers(TeamMemberDTO teamMemberDTO)
       {
           var teammebr = hcDc.hcTeamMembers.Where(n => n.TeamId == teamMemberDTO.TeamId && n.TeamMemberId == teamMemberDTO.TeamMemberId).SingleOrDefault();
           if (teammebr == null)
           {

               {
                   hcTeamMember teamMembr = new hcTeamMember


                     {
                         TeamId = teamMemberDTO.TeamId,
                         TeamMemberId = teamMemberDTO.TeamMemberId,
                         Description = teamMemberDTO.Description,
                         Role = teamMemberDTO.Role,
                         ModifiedBy = teamMemberDTO.ModifiedBy,
                         ModifiedTime = teamMemberDTO.ModifiedTime
                     };
                   hcDc.hcTeamMembers.InsertOnSubmit(teamMembr);

               }

               hcDc.SubmitChanges();

           }
       }


       public string SaveMatch(MatchDTO matchDTO)
       {
           string result ="";
           var match1 = hcDc.hcMatches.Where(n => n.MatchId == matchDTO.MatchId).SingleOrDefault();
           if (match1 == null)
           {
               var matchNme = hcDc.hcMatches.Where(a => a.MatchName == matchDTO.MatchName).SingleOrDefault();
               if (matchNme == null)
               {
                  try{

                   hcMatch match = new hcMatch
                   {
                       MatchId = matchDTO.MatchId,
                       MatchName = matchDTO.MatchName,
                       TeamId = matchDTO.TeamId,
                       OppositeTeam = matchDTO.OppositeTeamName,
                       HeldOn = matchDTO.HeldOn,
                       HeldAt = matchDTO.HeldAt,
                       Results = matchDTO.Results,
                       DetailedResults = matchDTO.DetailedResults,
                       TeamBestPerformerId = matchDTO.TeamBestPerformerId,
                       ModifiedBy = matchDTO.ModifiedBy,
                       ModifiedTime = matchDTO.ModifiedTime
                   };
                   hcDc.hcMatches.InsertOnSubmit(match);
                   result = "Saved";

                  }
                   catch
                   {
                       throw;
                   }

               }
               else
               {
                   //don't add if match name same
                   result ="AlreadyExists";
               }

           }
           else
           {
               //Edit
               result = "Updated";
           }

           hcDc.SubmitChanges();
           return result;
       }






       public List<TeamDTO> GetAllTeams()
       {
           return hcDc.hcTeams.Select(s => new TeamDTO
           {
               TeamId = s.TeamId,
               TeamName = s.AgeDivision + "  , " + s.GameType + "  Team - " + s.Year,
               AgeDivision = s.AgeDivision,
               Description = s.Description,
               GameType= s.GameType,
               TeamPhoto = s.TeamPhoto,
               Year = s.Year
           }).ToList();

       }

       public List<TeamDTO> GetTeamById(Guid teamId)
       {
           return hcDc.hcTeams.Where(a=> a.TeamId == teamId).Select(s => new TeamDTO
           {
               TeamId = s.TeamId,
               TeamName = s.AgeDivision + "  , " + s.GameType + "  Team - " + s.Year,
               AgeDivision =s.AgeDivision,
               Description = s.Description,
               GameType = s.GameType,
               TeamPhoto = s.TeamPhoto,
               Year = s.Year
           }).ToList();

       }


       public List<TeamDTO> GetTeamByGameTypeByYearByAgeGruop(string gameType, int year, string ageDivision)
       {
           List<TeamDTO> teamList = new List<TeamDTO>();
           if(gameType!="" && year!=0 && ageDivision!="")
           {
               List<hcTeam> teams = hcDc.hcTeams.Where(a => a.GameType == gameType && a.Year == year && a.AgeDivision == ageDivision).ToList();
               foreach(hcTeam team in teams)
               {
                   TeamDTO teamDTO = new TeamDTO();
                   teamDTO.AgeDivision = team.AgeDivision;
                   teamDTO.Description = team.Description;
                   teamDTO.GameType = team.GameType;
                   teamDTO.TeamId = team.TeamId;
                   teamDTO.TeamPhoto = team.TeamPhoto;
                   teamDTO.Year = team.Year;
                   teamList.Add(teamDTO);
               }
           }

           else if (gameType != "" && year != 0 )
           {
               List<hcTeam> teams = hcDc.hcTeams.Where(a => a.GameType == gameType && a.Year == year ).ToList();
               foreach (hcTeam team in teams)
               {
                   TeamDTO teamDTO = new TeamDTO();
                   teamDTO.AgeDivision = team.AgeDivision;
                   teamDTO.Description = team.Description;
                   teamDTO.GameType = team.GameType;
                   teamDTO.TeamId = team.TeamId;
                   teamDTO.TeamPhoto = team.TeamPhoto;
                   teamDTO.Year = team.Year;
                   teamList.Add(teamDTO);
               }
           }

           else if (gameType != ""  && ageDivision != "")
           {
               List<hcTeam> teams = hcDc.hcTeams.Where(a => a.GameType == gameType  && a.AgeDivision == ageDivision).ToList();
               foreach (hcTeam team in teams)
               {
                   TeamDTO teamDTO = new TeamDTO();
                   teamDTO.AgeDivision = team.AgeDivision;
                   teamDTO.Description = team.Description;
                   teamDTO.GameType = team.GameType;
                   teamDTO.TeamId = team.TeamId;
                   teamDTO.TeamPhoto = team.TeamPhoto;
                   teamDTO.Year = team.Year;
                   teamList.Add(teamDTO);
               }
           }

           else if (  year != 0 && ageDivision != "")
           {
               List<hcTeam> teams = hcDc.hcTeams.Where(a => a.Year == year && a.AgeDivision == ageDivision).ToList();
               foreach (hcTeam team in teams)
               {
                   TeamDTO teamDTO = new TeamDTO();
                   teamDTO.AgeDivision = team.AgeDivision;
                   teamDTO.Description = team.Description;
                   teamDTO.GameType = team.GameType;
                   teamDTO.TeamId = team.TeamId;
                   teamDTO.TeamPhoto = team.TeamPhoto;
                   teamDTO.Year = team.Year;
                   teamList.Add(teamDTO);
               }
           }

           else  if (gameType != "" )
           {
               List<hcTeam> teams = hcDc.hcTeams.Where(a => a.GameType == gameType).ToList();
               foreach (hcTeam team in teams)
               {
                   TeamDTO teamDTO = new TeamDTO();
                   teamDTO.AgeDivision = team.AgeDivision;
                   teamDTO.Description = team.Description;
                   teamDTO.GameType = team.GameType;
                   teamDTO.TeamId = team.TeamId;
                   teamDTO.TeamPhoto = team.TeamPhoto;
                   teamDTO.Year = team.Year;
                   teamList.Add(teamDTO);
               }
           }

           else  if ( year != 0 )
           {
               List<hcTeam> teams = hcDc.hcTeams.Where(a =>  a.Year == year ).ToList();
               foreach (hcTeam team in teams)
               {
                   TeamDTO teamDTO = new TeamDTO();
                   teamDTO.AgeDivision = team.AgeDivision;
                   teamDTO.Description = team.Description;
                   teamDTO.GameType = team.GameType;
                   teamDTO.TeamId = team.TeamId;
                   teamDTO.TeamPhoto = team.TeamPhoto;
                   teamDTO.Year = team.Year;
                   teamList.Add(teamDTO);
               }
           }

           else  if ( ageDivision != "")
           {
               List<hcTeam> teams = hcDc.hcTeams.Where(a =>  a.AgeDivision == ageDivision).ToList();
               foreach (hcTeam team in teams)
               {
                   TeamDTO teamDTO = new TeamDTO();
                   teamDTO.AgeDivision = team.AgeDivision;
                   teamDTO.Description = team.Description;
                   teamDTO.GameType = team.GameType;
                   teamDTO.TeamId = team.TeamId;
                   teamDTO.TeamPhoto = team.TeamPhoto;
                   teamDTO.Year = team.Year;
                   teamList.Add(teamDTO);
               }
           }
           return teamList;
       }


       public List<TeamMemberDTO> GetTeamMemberByTeamId(Guid teamId)
       {
          /* List<hcTeamMember> teamMembers = hcDc.hcTeamMembers.Where(a => a.TeamId == teamId).ToList();
           HcFacade hcFacade = new HcFacade();
           foreach (hcTeamMember teamMember in teamMembers)
           {
               TeamMemberDTO teamMemberDTO = new TeamMemberDTO();
               teamMemberDTO.TeamId = teamMember.TeamId;
               teamMemberDTO.TeamMemberId = teamMember.TeamMemberId;
               var teamMembr1 = hcDc.hcStudents.Where(a => a.StudentId == teamMember.TeamMemberId).SingleOrDefault();
               var teamMembr2 = hcDc.hcStaffs.Where(a => a.StaffId == teamMember.TeamMemberId).SingleOrDefault();
               if (teamMembr1 != null)
               {
                  teamMemberDTO.StudentFullName = teamMember.hcStudent.LastName+" "+teamMember.hcStudent.FirstName;
               }
               if (teamMembr2 != null)
               {
                   teamMemberDTO.StudentFullName = hcFacade.GetStaffById(teamMember.TeamMemberId).SingleOrDefault().FullName;
               }
               
               teamMemberDTO.Role = teamMember.Role;
               teamMemberDTO.Description = teamMember.Description;
           }
           return hcDc.hcTeamMembers.Where(a => a.TeamId == teamId).Select(s => new TeamMemberDTO
           {
               TeamId = s.TeamId,
               StudentFullName = s.hcStudent.LastName + s.hcStudent.FirstName,
               TeamMemberId = s.TeamMemberId,
               Role = s.Role,
               Description = s.Description,
               NavigateUrl = "viewStudentById.aspx?"+s.TeamMemberId
           }).ToList(); */


           List<TeamMemberDTO> teamMemberDTO = new List<TeamMemberDTO>();

           teamMemberDTO.AddRange(hcDc.hcTeamMembers.Where(a => a.TeamId == teamId && a.Role == "Captain").Select(s => new TeamMemberDTO
           {
               TeamId = s.TeamId,
               StudentFullName = s.hcStudent.LastName + s.hcStudent.FirstName,
               TeamMemberId = s.TeamMemberId,
               Role = s.Role,
               Description = s.Description,
               NavigateUrl = "viewStudentById.aspx?" + s.TeamMemberId
           }).ToList());

           teamMemberDTO.AddRange(hcDc.hcTeamMembers.Where(a => a.TeamId == teamId && a.Role == "Vice Captain").Select(s => new TeamMemberDTO
           {
               TeamId = s.TeamId,
               StudentFullName = s.hcStudent.LastName + s.hcStudent.FirstName,
               TeamMemberId = s.TeamMemberId,
               Role = s.Role,
               Description = s.Description,
               NavigateUrl = "viewStudentById.aspx?" + s.TeamMemberId
           }).ToList());

           teamMemberDTO.AddRange(hcDc.hcTeamMembers.Where(a => a.TeamId == teamId && a.Role == "Team Member").Select(s => new TeamMemberDTO
           {
               TeamId = s.TeamId,
               StudentFullName = s.hcStudent.LastName + s.hcStudent.FirstName,
               TeamMemberId = s.TeamMemberId,
               Role = s.Role,
               Description = s.Description,
               NavigateUrl = "viewStudentById.aspx?" + s.TeamMemberId
           }).ToList());

           teamMemberDTO.AddRange(hcDc.hcTeamMembers.Where(a => a.TeamId == teamId && a.Role == "Other Student's Role").Select(s => new TeamMemberDTO
           {
               TeamId = s.TeamId,
               StudentFullName = s.hcStudent.LastName + s.hcStudent.FirstName,
               TeamMemberId = s.TeamMemberId,
               Role = s.Role,
               Description = s.Description,
               NavigateUrl = "viewStudentById.aspx?" + s.TeamMemberId
           }).ToList());


           teamMemberDTO.AddRange(hcDc.hcTeamMembers.Where(a => a.TeamId == teamId && a.Role == "Coach").Select(s => new TeamMemberDTO
           {
               TeamId = s.TeamId,
               StudentFullName = s.hcStudent.LastName + s.hcStudent.FirstName,
               TeamMemberId = s.TeamMemberId,
               Role = s.Role,
               Description = s.Description,
               NavigateUrl = "viewStudentById.aspx?" + s.TeamMemberId
           }).ToList());

           teamMemberDTO.AddRange(hcDc.hcTeamMembers.Where(a => a.TeamId == teamId && a.Role == "Prefect Of Games(POG)").Select(s => new TeamMemberDTO
           {
               TeamId = s.TeamId,
               StudentFullName = s.hcStudent.LastName + s.hcStudent.FirstName,
               TeamMemberId = s.TeamMemberId,
               Role = s.Role,
               Description = s.Description,
               NavigateUrl = "viewStudentById.aspx?" + s.TeamMemberId
           }).ToList());

           teamMemberDTO.AddRange(hcDc.hcTeamMembers.Where(a => a.TeamId == teamId && a.Role == "Other Staff's role").Select(s => new TeamMemberDTO
           {
               TeamId = s.TeamId,
               StudentFullName = s.hcStudent.LastName + s.hcStudent.FirstName,
               TeamMemberId = s.TeamMemberId,
               Role = s.Role,
               Description = s.Description,
               NavigateUrl = "viewStudentById.aspx?" + s.TeamMemberId
           }).ToList());

           return teamMemberDTO;

       }



       public List<MatchDTO> GetAllMatchesByTeamId(Guid teamId)
       {
           return hcDc.hcMatches.Where(a=>a.TeamId== teamId).Select(s => new MatchDTO
           {
               TeamId = s.TeamId,
               MatchId = s.MatchId  ,
               MatchName = s.MatchName,
               HeldOn = s.HeldOn,
               HeldAt = s.HeldAt,
               OppositeTeamName = s.OppositeTeam,
               Results = s.Results,
               TeamBestPerformerId = s.TeamBestPerformerId,
               //TeamBestPerformerName = hcStudent.
               DetailedResults =s.DetailedResults,
               NavigateUrl = "viewMatchById.aspx?"+s.MatchId
           }).ToList();

       }

       public List<MatchDTO> GetMatchByMatchId(Guid matchId)
       { 
           //Here provided onnly one entry exists for the id this is true for match id team,staff
           List<hcStudent> student = new List<hcStudent>();
           List<hcStaff> staff = new List<hcStaff>();
           List<hcTeam> team = new List<hcTeam>();
           List<hcMatch> match =new List<hcMatch>();
           match= hcDc.hcMatches.Where(a=>a.MatchId == matchId).ToList();
           team = hcDc.hcTeams.Where(a=>a.TeamId == match[0].TeamId).ToList();
           student = hcDc.hcStudents.Where(a => a.StudentId == match[0].TeamBestPerformerId).ToList();

           List<MatchDTO> matchDTOList = new List<MatchDTO>();
           MatchDTO matchDTO = new MatchDTO();
          
              matchDTO.TeamId= match[0].TeamId;
              matchDTO.MatchId = match[0].MatchId;
               matchDTO.MatchName = match[0].MatchName;
               matchDTO.HeldOn = match[0].HeldOn;
               matchDTO.HeldAt = match[0].HeldAt;
               matchDTO.OppositeTeamName = match[0].OppositeTeam;
               matchDTO.Results = match[0].Results;
               matchDTO.TeamBestPerformerId = match[0].TeamBestPerformerId;
               //TeamBestPerformerName = hcStudent.
               matchDTO.DetailedResults = match[0].DetailedResults;
               matchDTO.NavigateUrl = "viewStudentById.aspx?" + student[0].StudentId;
               matchDTO.TeamBestPerformerName = student[0].LastName +" " + student[0].FirstName;
               matchDTO.TeamNavigationUrl = "viewTeamById.aspx?"+team[0].TeamId;
           matchDTO.ShortHeldOn = match[0].HeldOn.Value.ToShortDateString();
           matchDTO.TeamName = team[0].AgeDivision +" , " + team[0].GameType +" - " + team[0].Year;

           matchDTOList.Add(matchDTO);
           return matchDTOList;

       }

       public List<MatchDTO> GetAllMatches()
       {
           return hcDc.hcMatches.Select(s => new MatchDTO
           {
               TeamId = s.TeamId,
               MatchId = s.MatchId,
               MatchName = s.MatchName,
               HeldOn = s.HeldOn,
               HeldAt = s.HeldAt,
               OppositeTeamName = s.OppositeTeam,
               Results = s.Results,
               TeamBestPerformerId = s.TeamBestPerformerId,
               //TeamBestPerformerName = hcStudent.
               DetailedResults = s.DetailedResults,
               NavigateUrl = "viewMatchById.aspx?" + s.MatchId
           }).ToList();

       }


       public List<TeamMemberDTO> GetAllTeamParticipationOfTeamMemberByTeamMemberId(Guid teamMemberId)
       {
           return hcDc.hcTeamMembers.Where(a=>a.TeamMemberId == teamMemberId).Select(s => new TeamMemberDTO
           {
               TeamId = s.TeamId,
               TeamMemberId = s.TeamMemberId,
               Description = s.Description,
               ModifiedBy = s.ModifiedBy,
               Role = s.Role,
               ModifiedTime = s.ModifiedTime,
               NavigateUrl = "viewStudentById.aspx?" + s.TeamMemberId
           }).ToList();

       }
      
    }
}
